﻿using ML.Entity;
using ML.IBusiness;
using System.Text;
using System.Web.Http;

namespace ML.WebApi.Controllers
{
    /// <summary>
    /// 订单api接口
    /// </summary>
    [CrossSite]
    public class OrderController : BaseApiController
    {
        private readonly IOrderBusiness orderBusiness;
        public OrderController(IOrderBusiness orderBusiness)
        {
            this.orderBusiness = orderBusiness;
        }

        /// <summary>
        /// 前端已经在使用此接口—根据用户Id查询订单主表信息(关联商户以及订单子表信息)列表
        /// </summary>
        /// <param name="id">用户id</param>
        /// <returns></returns>
        [Route("api/order/getlistbyuserid")]
        [HttpGet]
        public IHttpActionResult GetListByUserId(string id)
        {
            var data = orderBusiness.GetListByUserId(id);
            return Json(data);
        }

        /// <summary>
        /// 前端已经在使用此接口—根据订单Id查询订单信息关联收货地址、和子表数据组合返回
        /// </summary>
        /// <param name="id">订单Id</param>
        /// <returns></returns>
        [Route("api/order/getdetailbyid")]
        [HttpGet]
        public IHttpActionResult GetDetailById(string id)
        {
            var data = orderBusiness.GetDetailById(id);
            return Json(data, null, Encoding.UTF8);
        }

        /// <summary>
        /// 前端已经在使用此接口—订单添加(用户从购物车将订单插入到订单表(主子表))
        /// </summary>
        /// <param name="cartOrders">订单插入数据</param>
        /// <returns></returns>
        [Route("api/order/add")]
        [HttpPost]
        public IHttpActionResult Add(CartOrders cartOrders)
        {
            var data = orderBusiness.Add(cartOrders);
            return Json(data);
        }

        /// <summary>
        /// 前端已经在使用此接口—根据订单Id更新订单状态
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [Route("api/order/updatestate")]
        [HttpPost]
        public IHttpActionResult UpdateState(OrderEntity entity)
        {
            var data = orderBusiness.UpdateState(entity.Id, entity.State);
            return Json(data);
        }

        /// <summary>
        /// 获取订单统计信息（今日、月）（微信小程序）
        /// </summary>
        /// <param name="id">商户id</param>
        /// <param name="type">类型：TODAY、MONTH</param>
        /// <returns></returns>
        [Route("api/wx/getordercount")]
        [HttpGet]
        public IHttpActionResult GetOrderCount(string id, string type)
        {
            return Json(orderBusiness.GetOrderStatistics(id, type));
        }

        /// <summary>
        /// 获取今日订单状态统计（微信小程序）
        /// </summary>
        /// <param name="id">商户id</param>
        /// <returns></returns>
        [Route("api/wx/gettodayorderstatus")]
        [HttpGet]
        public IHttpActionResult GetTodayOrderStatus(string id)
        {
            return Json(orderBusiness.GetTodayOrderStatus(id));
        }
    }
}